package com.geovis.emergency.spd.controller.mobile;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.geovis.common.auth.util.AuthUtil;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.pc.mapper.PcDrillPlanTaskMapper;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillPlanDetailService;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillPlanTaskApprovalService;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillPlanTaskService;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillService;
import com.geovis.emergency.spd.entity.mobile.pojo.vo.DrillPlanTaskMobileDetailVO;
import com.geovis.emergency.spd.entity.mobile.pojo.vo.DrillPlanTaskMobileFeedbackVO;
import com.geovis.emergency.spd.entity.mobile.pojo.vo.DrillPlanTaskMobileLogVO;
import com.geovis.emergency.spd.entity.mobile.pojo.vo.DrillPlanTaskMobileVO;
import com.geovis.emergency.spd.entity.pc.entity.PcDrill;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPlanDetail;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPlanTask;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPlanTaskApproval;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcDrillConstant;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcMessageConstant;
import com.geovis.emergency.spd.entity.pc.pojo.enums.TaskStatusEnum;
import com.geovis.emergency.spd.util.MsgUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：linwl
 * @date ：Created in 2022/6/17 16:38
 * @description：移动端-演练工作任务表
 * @version: v1
 */
@RestController
@RequestMapping("/mobile/pcDrillPlanTask")
@RequiredArgsConstructor
@Api(value = "移动端-应急预案指挥模块-任务相关接口", tags = "移动端-应急预案指挥模块-任务相关接口")
@Slf4j
public class PcDrillPlanTaskMobileController extends BaseController<IPcDrillPlanTaskService> {
    private final IPcDrillPlanTaskApprovalService pcDrillPlanTaskApprovalService;
    private final IPcDrillPlanDetailService pcDrillPlanDetailService;
    private final PcDrillPlanTaskMapper pcDrillPlanTaskMapper;
    private final IPcDrillService pcDrillService;
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "任务列表", notes = "传分页参数")
    @GetMapping("taskList")
    public Result<PageResult<DrillPlanTaskMobileVO>> taskList(@ApiParam(value = "演练id",name = "pcDrillId") String pcDrillId, @Validated PageParam pageParam) {
        String userId = AuthUtil.getCurrentUserId();
        PageResult<DrillPlanTaskMobileVO> pcDrillPageResult = pcDrillPlanDetailService.mobileTaskPage(pcDrillId,userId,pageParam);
        return Result.ok(pcDrillPageResult);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "任务详情",notes = "传任务id")
    @GetMapping("detail/{taskId}")
    public Result<DrillPlanTaskMobileDetailVO> detail(@PathVariable String taskId){
        DrillPlanTaskMobileDetailVO vo = baseService.detail(taskId);
        return Result.ok(vo);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "提交任务反馈",notes = "传DrillPlanTaskMobileFeedbackVO")
    @PostMapping("feedback")
    public Result<Void> feedback(@RequestBody @Validated DrillPlanTaskMobileFeedbackVO vo){

        PcDrillPlanDetail detail=pcDrillPlanDetailService.getById(vo.getTaskId());
        if(detail!=null)
        {
            PcDrill pcDrill=pcDrillService.getById(detail.getPcDrillId());
            if(PcDrillConstant.STATUS_COMPLETE.equals(pcDrill.getStatus()))
            {
                return Result.failed("演练已完成，无法提交");
            }
            if(PcDrillConstant.STATUS_PREPARE.equals(pcDrill.getStatus()))
            {
                return Result.failed("演练未启动，无法提交");
            }
        }
        PcDrillPlanTask planTask =  pcDrillPlanTaskMapper.selectByPlanId(vo.getTaskId());
        if(planTask!=null && TaskStatusEnum.WAIT_APPROVAL.getCode().equals(planTask.getStatus()))
        {
            return Result.failed("您已提交，请勿重复");
        }

        PcDrillPlanDetail pcDrillPlanDetail=baseService.feedback(vo,AuthUtil.getCurrentUserId());
        MsgUtil.sendDrillMsg(pcDrillPlanDetail.getPcDrillId(),null, PcMessageConstant.TYPE_PLAN_EXE_SUBMIT,AuthUtil.getCurrUser().getRealName()+"提交了任务反馈",vo.getTaskFeedback(),vo.getTaskId());
        return Result.ok();
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "任务记录",notes = "传任务id")
    @GetMapping("log/{taskId}")
    public Result<List<DrillPlanTaskMobileLogVO>> logList(@PathVariable String taskId){
        LambdaQueryWrapper<PcDrillPlanTaskApproval> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcDrillPlanTaskApproval::getPcDrillPlanDetailId,taskId);
        wrapper.orderByDesc(PcDrillPlanTaskApproval::getCreateTime);
        List<PcDrillPlanTaskApproval> list = pcDrillPlanTaskApprovalService.list(wrapper);
        return Result.ok(
        list.stream().map(e->{
            DrillPlanTaskMobileLogVO vo = new DrillPlanTaskMobileLogVO();
            vo.setLabel(e.getContent());
            vo.setTime(DateUtil.formatDateTime(Date.from(e.getCreateTime().atZone(ZoneId.systemDefault()).toInstant())));
            return vo;
        }).collect(Collectors.toList()));
    }

}
